<template>
  <div class="page-echarts">
    <div class="echarts" ref="echartsRef"></div>
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts'
import { ref, onMounted, watchEffect } from 'vue'
import type { EChartsCoreOption } from 'echarts'
import ChinaJSON from '../data/china.json'
echarts.registerMap('china', ChinaJSON as any)
const echartsRef = ref<HTMLElement>()
const props = defineProps<{
  options: EChartsCoreOption
}>()
onMounted(() => {
  const echartInstance = echarts.init(echartsRef.value!, 'light', {
    renderer: 'canvas'
  })
  // 刷新的时候数据会消失 所以需要监听数据的变化
  watchEffect(() => {
    echartInstance.setOption(props.options)
  })
  window.addEventListener('resize', () => {
    echartInstance.resize()
  })
})
</script>

<style lang="less" scoped>
.page-echarts {
  .echarts {
    width: 100%;
    height: 310px;
  }
}
</style>
